package com.qianfeng.smartdevice.controller;

import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.dto.UserDto;
import com.qianfeng.smartdevice.pojo.SysUserInfo;
import com.qianfeng.smartdevice.service.SysUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

 /**
 * @time: 2022-03-17 10:33
 */
@RestController
@RequestMapping("/users")
public class UserController {

    private SysUserInfoService userInfoService;

    @Autowired
    public void setUserInfoService(SysUserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    /**
     * 添加用户,注册
     * @param userDto
     * @return
     */
    @PostMapping("/user")
    public R registry(@RequestBody UserDto userDto) {
        //把userDto 转到sysUserInfo中
        SysUserInfo sysUserInfo = new SysUserInfo();
        BeanUtils.copyProperties(userDto,sysUserInfo);//拷贝对象,会将相同属性的内容复制到新的对象上面
        userInfoService.addUser(sysUserInfo);
        return R.setOK();
    }

    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto, HttpSession session) {
        SysUserInfo userInfo = userInfoService.login(userDto);
        session.setAttribute("user", userInfo);
        return R.setOK();
    }
}
